package org.speed.ddd;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

public class LogModel<T> {
	
	private String traceId;
	private Class clazzz;
	private String method;
	private Map<String,Object> params = new HashMap<>();
	private Map<String,Object> caller = new LinkedHashMap<>();
	private T result;
	
	LogModel(Class clazzz,String method) {
		String traceId = UUID.randomUUID().toString().replaceAll("-", "");
		this.traceId = traceId;
		this.clazzz = clazzz;
		this.method = method;
	}
	
	LogModel addParams(String key,Object value) {
		this.params.putIfAbsent(key, value);
		return this;
	}
	
	LogModel callSubLogs(String key,Object value) {
		this.caller.putIfAbsent(key, value);
		return this;
	}

	LogModel resultLog(T result) {
		this.result = result;
		return this;
	}
	
	

	@Override
	public String toString() {
		return "LogModel [traceId=" + traceId + ", clazzz=" + clazzz + ", method=" + method + ", params=" + params
				+ ", caller=" + caller + ", result=" + result + "]";
	}

	public static void main(String[] args) {
		LogModel logModel = new LogModel(EventPublisher.class,"createOrder");
		logModel.addParams("orderId", "10111111117");
		logModel.addParams("userId", "XXX10111111117");
		logModel.addParams("status", "1");
		
		logModel.callSubLogs("user.findUserId",String.format("params={} result={}", "a=1,b=1","xxxxxxxxx"));
		logModel.callSubLogs("pay.payOrder",String.format("params={} result={}", "a=1,b=1","xxxxxxxxx"));
		logModel.callSubLogs("order.updateStatus",String.format("params={} result={}", "a=1,b=1","xxxxxxxxx"));
		
		logModel.resultLog("ok");
		System.out.println(logModel);
		
	}
}
